package com.iocup.keybastion.spring.starter.config;


import com.ejlchina.okhttps.HTTP;
import com.iocup.keybastion.core.SecurityEngine;
import com.iocup.keybastion.sso.ClientElementProvider;
import com.iocup.keybastion.sso.ClientStoreService;
import com.iocup.keybastion.sso.ServerSsoAuthLogic;
import com.iocup.keybastion.sso.TicketService;
import com.iocup.keybastion.sso.config.SsoApiProperties;
import com.iocup.keybastion.sso.config.SsoProperties;
import com.iocup.keybastion.sso.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author xyjxust
 * @create 2022/3/23 14:40
 **/
@Configuration
@Conditional({EnabledAuthorizationServerCondition.class})

public class AuthorizationServerConfiguration {

    @Bean
    @ConditionalOnMissingBean(ServerSsoAuthLogic.class)
    public ServerSsoAuthLogic serverSsoAuthLogic(TicketService ticketService, SsoProperties ssoProperties,
                                                 SsoApiProperties ssoApiProperties,
                                                 ClientStoreService clientStoreService, HTTP http){
        ServerSsoAuthLogic serverSsoAuthLogic =  new ServerSsoAuthLogicImpl(ssoProperties,
                ticketService,ssoApiProperties,clientStoreService);
        serverSsoAuthLogic.setHttp(http);
        return serverSsoAuthLogic;
    }

    @Bean
    @ConditionalOnMissingBean(ClientStoreService.class)
    public ClientStoreService clientStoreService(SecurityEngine securityEngine){
        return new ClientStoreServiceImpl(securityEngine.getAuthenticationConfigurator().getLock(),
                securityEngine.getAuthenticationConfigurator().getCacheStore()
        );
    }


    @Bean
    @ConditionalOnMissingBean(TicketService.class)
    public TicketService ticketService(SecurityEngine securityEngine, SsoProperties ssoProperties){
        return new DefaultTicketService(securityEngine.getAuthenticationConfigurator().getLock(),
                ssoProperties,
                securityEngine.getAuthenticationConfigurator().getCacheStore()
        );
    }

    @Bean
    public ConfigFileClientElementProvider configFileClientElementProvider(SsoProperties ssoProperties){
        return new ConfigFileClientElementProvider(ssoProperties);
    }

    @Configuration
    @AutoConfigureAfter()
    public class  AfterConfig{


        @Autowired
        public void configMatch(ServerSsoAuthLogic serverSsoAuthLogic,SecurityEngine securityEngine){
            serverSsoAuthLogic.setLoginViewFunction(contextHolder -> new ModelAndView("login.html"));
            securityEngine.getAuthenticationConfigurator().getSecurityProperties().getExcludeList().add("/static/**");
        }
        @Autowired
        public void configClient(@Autowired List<ClientElementProvider> clientElementProviders, ServerSsoAuthLogic serverSsoAuthLogic){
            serverSsoAuthLogic.setClientService(new ClientServiceImpl(clientElementProviders));
        }

    }

}
